package com.intellij.GraphEditor;

import javax.swing.*;
import java.awt.event.*;

/**
 * Created by IntelliJ IDEA.
 * User: karpov
 * Date: 26.11.2006
 * Time: 12:29:27
 * To change this template use File | Settings | File Templates.
 */
public class MyMouseAdapter extends MouseAdapter {
    Editor Edlistener;
    String _name = "";
    String _comment="";

    public MyMouseAdapter(Editor form) {
        this.Edlistener = form;

    }

    public void mouseClicked(MouseEvent me) {
        switch (Editor.mode) {
            case 0:
                //TODO: select the node, edge
                break;
            case 1:
                object_full_write("Node name", me.getX(), me.getY(), Editor.mode);
                Editor.mode = 0;
                break;
            case 2:
                object_full_write("Edge name", me.getX(), me.getY(), Editor.mode);
                Editor.mode = 0;
                break;
            case 3:
                delete_node_full(me);
                Editor.mode = 0;
                break;
        }

    }

    public void delete_node_full(MouseEvent me) {
        for (int i = 0; i < Editor.vec.size(); i++) {
            Node temp = (Node) Editor.vec.elementAt(i);
            if (me.getX() < temp.coord_x + 10 && me.getX() > temp.coord_x &&
                    me.getY() < temp.coord_y + 10 && me.getY() > temp.coord_y) {
                temp.delete_node();
                Editor.vec.removeElementAt(i);
                break;
            }
        }
    }
    //TODO: drag and drop
//    public void mousePressed(MouseEvent me){
//        Edlistener.mouseX=me.getX();
//        Edlistener.mouseY=me.getY();
//        Edlistener.mousemsg="mouse at"+Edlistener.mouseX+","+Edlistener.mouseY;
////        editor.repaint_();
//        System.out.print("mouse");
//    }
//    public void  ris (Graphics g){
//        Edlistener.coord.setText("asdf");

    //    }


    void object_full_write(String frame_name, final int X, final int Y, int mode) {

        final Properties form = new Properties();
        final JFrame prop_frame = new JFrame(frame_name);

        prop_frame.setContentPane(form.name_panel);
        prop_frame.pack();

        prop_frame.setVisible(true);

        form.OKButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                _name = form.nameTextField.getText();
                _comment=form.write_comments_textArea.getText();
                if (!(_name.length() == 0)) {
                    if (unique_name(_name)) {


                        prop_frame.setVisible(false);

                        Node obj = new Node(X, Y, _name,_comment, Edlistener);
                        obj.draw_node();
                        Editor.vec.addElement(obj);
                        System.out.print("Vec size " + Editor.vec.size() + " ");
                    } else {
                        form.info_message.setText("Such name is already in use");
                    }

                } else {
                    form.info_message.setText("Name cant be empty");
                }
            }
        });

    }

    public boolean unique_name(String name_of_node) {
        boolean key = true;
        for (int i = 0; i < Editor.vec.size(); i++) {
            Node temp_node = (Node) Editor.vec.elementAt(i);

            if (name_of_node.equals(temp_node.object_name)) {
                key = false;
                break;
            }
        }
        return key;
    }

}


